rm(list=ls())
library(dplyr)
dat = read.csv("Figure1.csv", header=TRUE)
dat$country = as.character(dat$country)
width=3200
height=2400
res = 300
time = 2007:2016
##############################################################
# Create not.in function to make life easier
##############################################################
`%not.in%` <- function(x,y)!('%in%'(x,y))
##############################################################
# Delete Incomplete Countries
##############################################################
num.missing <- function(x) {
return (sum(is.na(x)))
}
dat.temp = dat
out = aggregate(x=dat.temp, by=list(country=dat.temp$country), FUN=num.missing)
has.missing = rowSums((out[,-1])) > 0
countries.without.missing = out$country[!has.missing]
dat = dat[dat$country %in% countries.without.missing,]
dat.muslim = dat[dat$muslim==1, ]
list.unique = c(unique(dat.muslim$country))
##############################################################
######### DRAW
##############################################################
draw <- function(x.range, ylim, ylab, dat1, dat2, dat3, dat4=NULL, legend.lab) {
plot(x.range, dat1$x,
type="l", ylim=ylim,
lty="solid",
ylab = ylab,
xlab ="Year",
lwd=4,
axes = FALSE,
cex.lab = 1.5
)
axis(side=1, at=seq(time[1], time[length(time)], 1))
axis(side=2, at=seq(ylim[1], ylim[2], 1))
lines(x.range, dat2$x, lwd=4, lty="dashed")
lines(x.range, dat3$x, lwd=4, lty="dotted")
#lines(x.range, dat4$x, lwd=4, lty="dotdash")
legend(x="topright",
legend=legend.lab,
#lty=c("solid","dashed","dotted","dotdash"),
lty=c("solid","dashed","dotted"),
col="black",
cex=1.4,bg="white",lwd=2)
}
##############################################################
# Plot
##############################################################
dependent = "SHI"
#christian-majority
agg.christian = aggregate( dat[dat$pctChristian>.5, dependent],
by=list(year=dat$year[dat$pctChristian>.5]),
mean, na.rm=TRUE)
#muslim-majority
agg.muslim = aggregate( dat[dat$pctMuslim>=.5, dependent],
by=list(year=dat$year[dat$pctMuslim>=.5]),
mean, na.rm=TRUE)
#nonmuslim-majority
agg.nonmuslim = aggregate( dat[dat$pctMuslim<.5, dependent],
by=list(year=dat$year[dat$pctMuslim<.5]),
mean, na.rm=TRUE)
#non-muslim, non-Europe-majority
agg.nonwestern = aggregate( dat[dat$pctMuslim<.5 & dat$Region6 %not.in% c("Europe","North America"), dependent],
by=list(year=dat$year[dat$pctMuslim<.5  & dat$Region6 %not.in% c("Europe","North America")]),
mean, na.rm=TRUE)
png(filename = "Figure 1. Trends of Social Hostilities Index.png", width=width, height=height, res=res)
draw(x.range = time,
ylim = c(0,10),
ylab = "Social Hostilities",
dat1 = agg.muslim,
dat2 = agg.christian,
dat3 = agg.nonmuslim,
dat4 = agg.nonwestern,
legend.lab = c("Muslim-Majority Countries","Christian-Majority Countries","Non-Muslim Majority Countries")
#legend.lab = c("Muslim Countries","Christian Countries","Non-Muslim Countries","Non-Western, Non-Muslim Countries")
)
dev.off()
######################################################
##### Plot All
######################################################
rm(list = ls())
dat = read.csv("Figure2.csv", header=TRUE)
png("Figure 2. Religious Bonding in Muslim- and Catholic-Majority Countries.png", width=4000, height=2000, res=300)
plot(dat$rdi2010[dat$muslim==1], dat$bonding1[dat$muslim==1], xlab="Religious Diversity Index", ylab="Close Friends with Similar Religion", cex.lab=1.6, cex.axis=1.6,
xlim=c(-.05, 6.5), ylim=c(3,5), col="dark green", yaxt='n')
points(dat$rdi2010[dat$muslim==0], dat$bonding1[dat$muslim==0], col="blue", pch=2)
axis(2,at=seq(3,5),labels=c("Some","Most","All"), cex.axis=1.6)
text(x=dat$rdi2010[dat$muslim==1], y=dat$bonding1[dat$muslim==1], labels=dat$countrycode[dat$muslim==1], col="dark green", cex=1.3, pos=4)
text(x=dat$rdi2010[dat$muslim==0], y=dat$bonding1[dat$muslim==0], labels=dat$countrycode[dat$muslim==0], col="blue", cex=1.3, pos=4)
meanMuslim = mean(dat$bonding1[dat$muslim==1], na.rm=TRUE)
meanCatholic = mean(dat$bonding1[dat$muslim==0], na.rm=TRUE)
text(5.5, meanMuslim+.01, labels="Mean of Muslim countries", cex=1.1, col="dark green")
text(5.5, meanCatholic, labels="Mean of Catholic countries", cex=1.1, col="blue")
abline(a=meanMuslim,b=0,lty=2,col="dark green")
abline(a=meanCatholic,b=0,lty=2,col="blue")
dev.off()
